/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/

package com.genia.toolbox.web.io;

import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.servlet.ServletContext;

/**
 * This class wrapps a {@link ServletContext} and allows to modify the
 * attributes of the context without modifying the underlying context.
 */
public class ServletContextProxy
    extends ServletContextWrapper
{

  /**
   * Constructor.
   * 
   * @param servletContext
   *          the proxied {@link ServletContext}
   */
  public ServletContextProxy(ServletContext servletContext)
  {
    super(servletContext);
  }

  /**
   * the attributes added to this session.
   */
  private transient final Map<String, Object> attributes = new TreeMap<String, Object>();

  /**
   * the attributes removed from this session.
   */
  private transient final Set<String> removedAttributes = new TreeSet<String>();



  /**
   * Returns the servlet container attribute with the given name, or
   * <code>null</code> if there is no attribute by that name. An attribute
   * allows a servlet container to give the servlet additional information not
   * already provided by this interface. See your server documentation for
   * information about its attributes. A list of supported attributes can be
   * retrieved using <code>getAttributeNames</code>.
   * <p>
   * The attribute is returned as a <code>java.lang.Object</code> or some
   * subclass. Attribute names should follow the same convention as package
   * names. The Java Servlet API specification reserves names matching
   * <code>java.*</code>, <code>javax.*</code>, and <code>sun.*</code>.
   * 
   * @param name
   *          a <code>String</code> specifying the name of the attribute
   * @return an <code>Object</code> containing the value of the attribute, or
   *         <code>null</code> if no attribute exists matching the given name
   * @see javax.servlet.ServletContext#getAttribute(java.lang.String)
   */
  @Override
  public Object getAttribute(final String name)
  {
    return ProcessProxy.getValue(name, attributes, removedAttributes, super.getAttribute(name));
  }



  /**
   * Returns an <code>Enumeration</code> containing the attribute names
   * available within this servlet context. Use the {@link #getAttribute} method
   * with an attribute name to get the value of an attribute.
   * 
   * @return an <code>Enumeration</code> of attribute names
   * @see javax.servlet.ServletContext#getAttributeNames()
   */
  @SuppressWarnings("unchecked")
  @Override
  public Enumeration<String> getAttributeNames()
  {
    return ProcessProxy.getNames(attributes, removedAttributes, (Enumeration<String>) super.getAttributeNames());
  }



  /**
   * Removes the attribute with the given name from the servlet context. After
   * removal, subsequent calls to {@link #getAttribute} to retrieve the
   * attribute's value will return <code>null</code>.
   * <p>
   * If listeners are configured on the <code>ServletContext</code> the
   * container notifies them accordingly.
   * 
   * @param name
   *          a <code>String</code> specifying the name of the attribute to be
   *          removed
   * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
   */
  @Override
  public void removeAttribute(final String name)
  {
    removedAttributes.add(name);
    attributes.remove(name);
  }



  /**
   * Binds an object to a given attribute name in this servlet context. If the
   * name specified is already used for an attribute, this method will replace
   * the attribute with the new to the new attribute.
   * <p>
   * If listeners are configured on the <code>ServletContext</code> the
   * container notifies them accordingly.
   * <p>
   * If a null value is passed, the effect is the same as calling
   * <code>removeAttribute()</code>.
   * <p>
   * Attribute names should follow the same convention as package names. The
   * Java Servlet API specification reserves names matching <code>java.*</code>,
   * <code>javax.*</code>, and <code>sun.*</code>.
   * 
   * @param name
   *          a <code>String</code> specifying the name of the attribute
   * @param value
   *          an <code>Object</code> representing the attribute to be bound
   * @see javax.servlet.ServletContext#setAttribute(java.lang.String,
   *      java.lang.Object)
   */
  @Override
  public void setAttribute(final String name, final Object value)
  {
    removedAttributes.remove(name);
    attributes.put(name, value);
  }

}
